package core.tool.common;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;


public class DateHelper {
	
    public static boolean isEmpty(String value){  
        if(value == null || "".equals(value)){  
            return true;  
        }  
        return false;  
    } 
	
	public static String getFormatDate(Date date,String format) {
		if(date==null) return null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
    
	
    public static boolean isDate(String value,String format){  
        
        SimpleDateFormat sdf = null;  
        ParsePosition pos = new ParsePosition(0);//指定从所传字符串的首位开始解析  
        
        if(isEmpty(value) || isEmpty(format)){  
            return false;  
        }  
        try {  
            sdf = new SimpleDateFormat(format);  
            sdf.setLenient(false);  
            Date date = sdf.parse(value,pos);  
            if(date == null){  
                return false;  
            }else{  
                //更为严谨的日期,如2011-03-024认为是不合法的  
                if(pos.getIndex() > sdf.format(date).length()){  
                    return false;  
                }  
                return true;  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
    }
    
    public static boolean isDateAllFormat(String value){
    	String format1="yyyy-MM-dd";
    	String format2="yyyy/MM/dd";
    	String format3="yyyy-MM-dd HH:mm:ss";
    	String format4="yyyy-MM-dd HH:mm:ss";
    	boolean flag1=isDate(value,format1);
    	boolean flag2=isDate(value,format2);
    	boolean flag3=isDate(value,format3);
    	boolean flag4=isDate(value,format4);
    	boolean result=flag1 || flag2 || flag3 || flag4;   	
    	return result;
    }  
    
    
    public static Date getDate(String ss) {
    	Date date=null;
//    	 2013-04-08 11:45
		String[] pattern = new String[]{"yyyy-MM","yyyyMM","yyyy/MM",   
                "yyyyMMdd","yyyy-MM-dd","yyyy/MM/dd",   
                "yyyyMMddHHmmss",   
                            "yyyy-MM-dd HH:mm:ss",
                            "yyyy-MM-dd HH:mm",
                            "yyyy/MM/dd HH:mm:ss"};   
		try {
			date=DateUtils.parseDate(ss, pattern);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   	
    	return date;
    }
}
